/*
 * Copyright 2010 Quytelda Gaiwin
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HelpDialog.java
 *
 * Created on Dec 17, 2008, 8:47:28 PM
 */
package panthersleek;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Quytelda K. Gaiwin
 */
public class HelpDialog extends javax.swing.JDialog
{

    /**
     * Creates new form HelpDialog
     *
     * @param parent the parent frame, may be null
     * @param modal  whether or not the dialog is modal
     */
    public HelpDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();

        helpPagePane.setEditable(false);

        //Get the desktop environment.
        desktop = Desktop.getDesktop();

        //Set the starting page.
        try
        {
            //Display the help pages in the Editor pane.
            helpPagePane.setPage(HelpDialog.class.getResource("/panthersleek/resources/help/index.html"));
        }
        catch (IOException ex)
        {
            Logger.getLogger(HelpDialog.class.getName()).log(Level.SEVERE, null, ex);
        }

        helpPagePane.addHyperlinkListener(new HyperlinkListener()
        {
            public void hyperlinkUpdate(HyperlinkEvent evt)
            {
                URL url = evt.getURL();
                try
                {
                    if (evt.getEventType().equals(EventType.ACTIVATED))
                    {
                        if (url.getHost().startsWith("www."))
                        {
                            //Launch the URL in the Default Browser.
                            if (Desktop.isDesktopSupported())
                            {
                                URI page = null;
                                try
                                {
                                    page = new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
                                }
                                catch (URISyntaxException ex)
                                {
                                    ex.printStackTrace();
                                }
                                desktop.browse(page);
                            }
                        }
                        else if (url.getHost().startsWith("mailto:"))
                        {
                            //Launch the default mail program.
                            URI mailtoAddress = null;

                            try
                            {
                                mailtoAddress = new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
                            }
                            catch (URISyntaxException ex)
                            {
                                ex.printStackTrace();
                            }

                            desktop.mail(mailtoAddress);
                        }
                        else
                        {
                            helpPagePane.setPage(url);
                        }
                    }
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     */
    @SuppressWarnings("unchecked")
    private void initComponents()
    {

        JScrollPane documentScrollPane = new JScrollPane();
        helpPagePane = new javax.swing.JEditorPane();
        JButton closeButton = new JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Help");

        documentScrollPane.setViewportView(helpPagePane);

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                closeButtonActionPerformed();
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(documentScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                                .addComponent(closeButton))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(documentScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton)
                        .addContainerGap())
        );

	this.setSize(new Dimension(700, 400));
    }

    private void closeButtonActionPerformed()
    {
        this.setVisible(false);
    }

    private JEditorPane helpPagePane;
    private Desktop desktop;
}
